<?php
import( 'de.ceus-media.framework.krypton.core.Logic' );
import( 'classes.model.Topic' );
import( 'classes.model.Record' );
import( 'classes.logic.Authentication' );
//import( 'de.ceus-media.framework.krypton.core.database.StatementBuilder' );
/**
 *	Logic of Result Records.
 *	@package		mv2.logic
 *	@extends		Framework_Krypton_Core_Logic
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@author			David Seebacher <dseebacher@gmail.com>
 *	@since			13.03.2007
 *	@version		0.1
 */
/**
 *	Logic of Result Records.
 *	@package		mv2.logic
 *	@extends		Framework_Krypton_Core_Logic
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@author			David Seebacher <dseebacher@gmail.com>
 *	@since			13.03.2007
 *	@version		0.1
 */
class Logic_Record extends Framework_Krypton_Core_Logic
{
	function addRecord( $resultId, $status, $comment, $attachmentId = false )
	{
		$resultData	= Logic_Result::getResultData( $resultId );
		if( !$resultData )
			throw new Framework_Krypton_Exception_Logic( 'invalid_result_id' );
		$statusOld	= $resultData['status'];

		$data	= array(
			'tester_id'		=> Logic_Authentication::getCurrentUser(),
			'result_id'		=> $resultId,
			'status_old'	=> $statusOld,
			'status_new'	=> $status,
			'comment'		=> $comment,
		);
		if( $attachmentId )
			$data['attachment_id']	= $attachmentId;
		$record	= new Model_Record();
		return $record->insert( $data, false );
	}

	static function getRecordAttachmentData( $recordId )
	{
		$record		= self::getRecordData( $recordId );
		return  Logic_Attachment::getAttachmentData( $data['attachment_id'] );
	}

	static function getRecordAttachmentUri( $recordId )
	{
		$data	= self::getRecordAttachmentData( $recordId );
		return Logic_Attachment::getAttachmentUri( $data['attachment_id'] );
	}

	static function getRecordAttachmentTitle( $recordId )
	{
		$data	= self::getRecordAttachmentData( $recordId );
		return Logic_Attachment::getAttachmentTitle( $data['attachment_id'] );
	}

	static function getRecordAttachmentDescription( $recordId )
	{
		$data	= self::getRecordAttachmentData( $recordId );
		return Logic_Attachment::getAttachmentDescription( $data['attachment_id'] );
	}

	static function getRecordList( $resultId, $filters = array(), $orders = array() )
	{
		$model	= new Model_Record();
		$conditions	= array_merge( array( 'result_id' => $resultId ), $filters );
		return $model->find( array(), $conditions, $orders );
	}

	static function getRecordData( $recordId )
	{
		$model	= new Model_Record( $recordId );
		$data	= $model->get();
		if( !$data )
			throw new Framework_Krypton_Exception_Logic( 'invalid_record_id' );
		return $data;
	}

	static function getRecordComment( $recordId )
	{
		$data	= self::getRecordData( $recordId );
		return $data['description'];
	}

	static function getRecordTitle( $recordId )
	{
		$data	= self::getRecordData( $recordId );
		return $data['title'];
	}
}
?>